package com.lsege.controller.backstage;

import com.github.pagehelper.PageHelper;
import com.lsege.entity.JsonResult;
import com.lsege.entity.Page;
import com.lsege.entity.backstage.MerchantOccupancyOrder;
import com.lsege.service.backstage.MerchantOccupancyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * Created by Administrator on 2017/8/1 0001.
 */
@RestController
@RequestMapping("/MerchantOccupancy")
public class MerchantOccupancyController {
    @Autowired
    MerchantOccupancyService merchantOccupancyService;

    /**
     * 获取商家入驻申请列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("getMerchantOccupancyOrder")
    public JsonResult getMerchantOccupancyOrder(Integer pageNum, Integer pageSize){
        JsonResult json=new JsonResult();
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<MerchantOccupancyOrder> list=merchantOccupancyService.getMerchantOccupancyOrder();
            Long total=merchantOccupancyService.getOrderCount();
            json.setData(new Page(list,total,pageSize,pageNum));
            json.setSuccess(true);
            json.setMessage("获取列表成功");
        }catch (Exception e){
            e.printStackTrace();
            json.setSuccess(false);
            json.setMessage("获取列表失败");
        }
        return json;
    }

    /**
     * 修改入驻状态
     * @param merchantOccupancyOrder
     * @return
     */
    @PostMapping("toPayOver")
    public JsonResult toPayOver(MerchantOccupancyOrder merchantOccupancyOrder){
        JsonResult json=new JsonResult();
        Integer state=merchantOccupancyService.getStateByCode(merchantOccupancyOrder);
        if (merchantOccupancyOrder.getOrderState()==null){
            json.setSuccess(false);
            json.setMessage("请选择要修改的状态");
        }else{
            if(state==Integer.valueOf(merchantOccupancyOrder.getOrderState())){
                json.setSuccess(false);
                json.setMessage("该商家的状态无需修改");
            }else {
                try {
                    merchantOccupancyService.toPayOver(merchantOccupancyOrder);
                    json.setSuccess(true);
                    json.setMessage("修改成功");
                }catch (Exception e){
                    e.printStackTrace();
                    json.setSuccess(false);
                    json.setMessage("修改失败");
                }
            }
        }
        return json;
    }



    /**
     * 查看某个入驻订单
     * @param checkInCode
     * @return
     */
    @GetMapping("getOneJoinOrder")
    public JsonResult getOneJoinOrder(String checkInCode){
        JsonResult json=new JsonResult();
        try{
            List<MerchantOccupancyOrder> moo=merchantOccupancyService.getOneJoinOrder(checkInCode);
            if (moo.size()==0){
                json.setSuccess(false);
                json.setMessage("您所输入的入驻编号不存在");
            }else {
                json.setData(moo);
                json.setSuccess(true);
                json.setMessage("获取成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            json.setSuccess(false);
            json.setMessage("获取失败");
        }
        return json;
    }

}
